<?php
namespace Member\Controller;
use Think\Controller;
class RegController extends Controller {
    public function login(){
        $this->display();
    }
    public function verify(){
		ob_clean();
        $config =    array(
            'fontSize'    =>    12,
            'length'      =>    3,
            'useNoise'    =>    false,
            'bg'=>array(229,229,229),
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    public function verify_check($code,$id){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
	//注册
	public function registerD($company,$username,$pwd,$pwd2,$email){
        if($company==''){
            alert(0,'企业名称不能为空');
        }
        if($username==''){
            alert(0,'手机号不能为空');
        }
        if($pwd==''){
            alert(0,'密码不能为空');
        }
        if($pwd2!=$pwd){
            alert(0,'两次密码不相同');
        }
        if($email==''){
            alert(0,'E-mail不能为空');
        }
        // if($name==''){
            // alert(0,'姓名不能为空');
        // }
        if(!email($email)){
            alert(0,'邮箱地址错误');
        }
        if(!mobile($username)){
            alert(0,'手机号码错误');
        }
        if(M("Member")->where("username='".$username."'")->count()>=1 or M("user")->where("username='".$username."'")->count()>=1){
            alert(0,'手机号已经存在！');
        }
        if(M("Member")->where("company='".$company."'")->count()>=1 or M("user")->where("company='".$company."'")->count()>=1){
            alert(0,'企业名称已经存在！');
        }
        if(M("Member")->where("email='".$email."'")->count()>=1 or M("user")->where("email='".$email."'")->count()>=1){
            alert(0,'电子邮箱已经存在！');
        }
        $data['salt']=rand(10000,99999);
        $data['username']=$username;
        $data['company']=$company;
        $data['password']=md5($pwd.$data['salt']);
		$data['erp_has']=1;
        // $data['name']=$name;
        $data['email']=$email;
        $result=M("Member")->add($data);
        if($result){
			unset($data['erp_has']);
			$data['member_id']=$result;
			$result_user=M("user")->add($data);
			if($result_user){
				$reg=A('Company/Reg');
				if($reg->createDb(C('DB_PREFIX_T').$username)){
				   alert(1,'注册成功');
			    }else{
				   alert(0,'注册失败，请重试');
			    }
			}
        }else{
            alert(0,'注册失败，请重试');
        }
    }
    public function loginD($username,$password,$verify){
        if(!$this->verify_check($verify)){
            alert(0,'验证码错误');
        }
        if($username=='' || $password==''){
            alert(0,'用户名或者密码不能为空');
        }

        $data=M("Member")->where(array('username'=>$username))->find();
		$result=M('user')->where(array('username'=>$username))->find();
		$result_warehouse=M('Warehouse')->where(array('member_id'=>$data['id']))->find();
		
		
        if(md5($password.$data['salt'])==$data['password']){
            session('member',$data);
            session('type',$data['type']);
            $s=array('jq'=>'jiangqiao');
            session('jq',$s);
            $d['last_login_date']=date("Y-m-d H:i:s",time());
            $d['login_count']=$data['login_count']*1+1;
            M("member")->where(array('username'=>$username))->save($d);
			//erp-登录-begin
			$result['db']=C('DB_PREFIX_T').$result['username'];
            $result['type']=1;
            $result['company_id']=$result['id'];
            session('company',$result);
			session('username',$result_warehouse['username']);
			//erp-登录-begin
            alert(1, '登录成功');
        }else{
            alert(0, '登录失败');
        }
    }
    public function exits(){
        session('member',null);
        session('type',null);
        $this->redirect('Reg/login');
    }

}